package com.springboot.cloud.demos.producer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.demos.entity.param.AlbumQueryParam;
import com.springboot.cloud.demos.entity.po.Album;
import com.springboot.cloud.demos.producer.dao.AlbumMapper;
import com.springboot.cloud.demos.producer.service.IAlbumService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
@RefreshScope
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, Album> implements IAlbumService {

    @Value("${producer.album:123}")
    private String value;

    @Override
    @CacheEvict(value = "album", allEntries = true)
    public boolean add(Album album) {
        return this.save(album);
    }

    @Override
    @CacheEvict(value = "album", allEntries = true)
    public boolean batchAdd(List<Album> albums) {
        return this.saveBatch(albums);
    }


    @Override
    @CacheEvict(value = "album", allEntries = true)
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @CacheEvict(value = "album", allEntries = true)
    public void deleteByUrl(String url) {
//        QueryWrapper<Album> queryWrapper = new QueryWrapper();
//        queryWrapper.like("url", url);
//        List<Album> albums = this.list(queryWrapper);
//        if (!albums.isEmpty()) {
//            Album album = albums.get(0);
//            album.setDeleted("Y");
//            this.update(album);
//        }
        this.getBaseMapper().deleteByUrl(url);
    }

    @Override
    public boolean update(Album album) {
        return this.updateById(album);
    }

    @Override
    public Album get(String id) {
        log.info("value:{}", value);
        return this.getById(id);
    }

    @Override
    @Cacheable(value = "album", key = "#root.targetClass+'-query-'+#param.momentId")
    public List<Album> query(AlbumQueryParam param) {
        QueryWrapper<Album> queryWrapper = param.build();
        if (param.getMomentId() != null) {
            queryWrapper.eq("moment_id", param.getMomentId());
        }
        if (param.getUserId() != null) {
            queryWrapper.eq("user_id", param.getUserId());
        }
        return this.list(queryWrapper);
    }


}
